// Replication code for Analysis of Secondary Migration (Part A)
// 07/01/2020


// load refugee data
clear
set matsize 11000
use "REFlprNATZv2.dta", clear

// prepare merge with state level measures

* drop if arrival state (i.e. rstate) is missing and or outside of scope
gen     state= rstate
drop if state==""
drop if state=="PR" | state=="WY" | state=="GU"


// merge in regional data based on arrival year (i.e. rarrivefy) and state
gen year       = rarrivefy 
merge m:1 state year using "tomerge_state4.dta" 

* keep matched cases
keep if _merge==3

* drop arrival city (rcity) and arrival state combinations that are errors
gen   city_upper = rcity
drop if city_upper=="PHOENIX" & state =="AK"
drop if city_upper=="PHOENIX" & state =="AR"
drop if city_upper=="CHESTER HILLS" & state =="MI"
drop if city_upper=="CORONAIDE PARK" & state =="NY"
drop if city_upper=="SAN JOSE" & state =="CT"
drop if city_upper=="SAN FRANCISCO" & state =="CO"
drop if city_upper=="SACRAMENTO" & state =="WA"
drop if city_upper=="DES MOINES" & state =="LA"
drop if city_upper=="NEW" & state =="LA"
drop if city_upper=="COLORADO" & state =="CO"
drop if city_upper=="NEW SMYRNA BEACH" & state =="DE"
drop if city_upper=="CARBONDALE" & state =="MO"
drop if city_upper=="DES PLAINES" & state =="IN"
drop if city_upper=="INDIANAPOLIS" & state =="MN"
drop if city_upper=="DECATUR" & state =="CO"
drop if city_upper=="GREENFIELD" & state =="MD"
drop if city_upper=="OVERLAND PARK" & state =="MO"
drop if city_upper=="WEST" & state =="MA"
drop if city_upper=="OMAHA" & state =="NH"
drop if city_upper=="NASHUA" & state =="MA"
drop if city_upper=="FOREST HILLS" & state =="NV"
drop if city_upper=="NEW BERN" & state =="SC"
drop if city_upper=="NORTH" & state =="CA"
drop if city_upper=="OHIO" & state =="OH"
drop if city_upper=="OKNEY" & state =="MA"
drop if city_upper=="SOUTH DAKOTA" & state =="SD"
drop if city_upper=="ARLINGTON" & state =="CT"
drop if city_upper=="GRANOF" & state =="VA"
drop if city_upper=="VIRGINIA" & state =="VA"
drop if city_upper=="BURIEN" & state =="VA"
drop if city_upper=="TRI CITIES" & state =="WA"
drop if city_upper=="MILWAUKI" & state =="MI"

/// Sample restrictions

// drop if gender undefined
drop if rgender == "U"

// drop missing arrival age
drop if rage==.

// drop post 2014 arrivals
drop if rarrivefy>2014

// drop refugees who are below 18 years of age upon arrival
drop if rage < 18

// drop if cuban
drop if rnationality=="CUBA"

/// code indicator for match between arrival and LPR data
gen     match1_r_l = 0 if flag_lpr == . | flag_lpr == 0  
replace match1_r_l = 1 if flag_lpr == 1 | flag_lpr==2 | flag_lpr == 3

/// code outcome (move between arrival and LPR state)

// moved out of state

gen     movedrtol = rstate != lstate
replace movedrtol = . if lstate==""

/// code predictors

// relationship
gen     relat="CHILD" 	    if rrelationship =="CHILD"
replace relat="PA" 			if rrelationship =="PA"
replace relat="SPOUSE" 		if rrelationship =="SPOUSE"
replace relat="PARENT" 		if rrelationship =="PARENT"
replace relat="SIBLING" 	if rrelationship =="SIBLING"
replace relat="OTHER" 		if rrelationship =="OTHER" | rrelationship =="EXSPOUSE" | rrelationship =="UNION"
encode  relat , gen(relat_n)
replace relat_n = 99 if relat=="PA"

// top 15 nationalities
bys  rnationality: gen rnat_count = _N
gen  rnat_countneg = -1*rnat_count
sort rnat_countneg rnationality , stable
egen rnat_rank1 = group(rnat_countneg rnationality)
gen     rnat_top    = rnationality
replace rnat_top    = "Other" if rnat_rank1 > 15
encode  rnat_top , gen(rnat_top_n)
replace rnat_top_n = 99 if  rnat_top    == "Other"

* education
gen     reduccat = "None"                if reducation=="" | reducation=="UNKNOWN" | reducation=="NONE"
replace reduccat = "Less than Secondary" if reducation=="KINDERGARTEN" | reducation=="PRIMARY" | reducation=="INTERMEDIATE"
replace reduccat = "Secondary"           if reducation=="SECONDARY" 
replace reduccat = "PreUniversity"       if reducation=="TECHNICAL SCHOOL" | reducation=="PRE-UNIVERSITY" | reducation=="PROFESSIONAL"
replace reduccat = "University"          if reducation=="UNIVERSITY/COLLEGE" | reducation=="GRADUATE SCHOOL" 
encode  reduccat  , gen(reduccat_n)
replace reduccat_n = 99 if  reduccat == "None" 

* agency
encode  rra  , gen(rra_n)
replace rra_n = 99 if  rra == "HIAS" 

* US Ties
cap noisily drop free
gen      free_n = 1 if rpool == "NO U.S. TIES CASES"
replace  free_n = 99 if rpool != "NO U.S. TIES CASES" 

* gender
encode  rgender , gen(rgender_n)
replace rgender_n = 99 if rgender=="M"

* age
gen     rage_n = 99 if rage>=18 & rage<=20
replace rage_n = 1  if rage>=21 & rage<=30
replace rage_n = 2  if rage>=31 & rage<=40
replace rage_n = 3  if rage>=41 & rage<=50
replace rage_n = 4  if rage>=51 & rage<=60
replace rage_n = 5  if rage>=61 

* year of arrival
gen      rarrivefy_n = rarrivefy
replace  rarrivefy_n = 99 if rarrivefy==2000

* state
encode rstate, gen(rstate_n)
bys rstate: gen rstate_count = _N
replace rstate_n = 99 if rstate=="PA"

* case size
bys     rcasenumber: gen casesize_n = _N
replace casesize_n = 4  if casesize_n >=4 & casesize_n~=.
replace casesize_n = 99 if casesize_n==1

/// recode state level factors

* share of co-nationals
gen     coshare = .
replace coshare = nat_afghan    if rnat_top=="AFGHANISTAN"            & coshare==.
replace coshare = nat_bhutan    if rnat_top=="BHUTAN"                 & coshare==.
replace coshare = nat_bosnia    if rnat_top=="BOSNIA AND HERZEGOVINA" & coshare==.
replace coshare = nat_burma     if rnat_top=="BURMA"                  & coshare==.
replace coshare = nat_drc       if rnat_top=="DEM. REP. CONGO"        & coshare==.
replace coshare = nat_ethiopia  if rnat_top=="ETHIOPIA"               & coshare==.
replace coshare = nat_eritrea   if rnat_top=="ERITREA"                & coshare==.
replace coshare = nat_iran      if rnat_top=="IRAN"                   & coshare==.
replace coshare = nat_iraq      if rnat_top=="IRAQ"                   & coshare==.
replace coshare = nat_liberia   if rnat_top=="LIBERIA"                & coshare==.
replace coshare = nat_russia    if rnat_top=="RUSSIA"                 & coshare==.
replace coshare = nat_somalia   if rnat_top=="SOMALIA"                & coshare==.
replace coshare = nat_sudan     if rnat_top=="SUDAN"                  & coshare==.
replace coshare = nat_vietnam   if rnat_top=="VIETNAM"                & coshare==.
replace coshare = nat_ukraine   if rnat_top=="UKRAINE"                & coshare==.

* State welfare spending per per poor individual
gen  expend_welfare_poorpop = expend_welfare/totalpoor

* State welfare spending per capita 
gen  expend_welfare_pop     = expend_welfare/population

* TANF rate 
gen  tanf_benefit_scaled    = (tanf_benefit_rate*12)/col_household_expend

* Share of Dem Seats (Both Chambers)
egen dem_share_both         = rowmean(dem_share_sen dem_share_house)

* created five bins
foreach x of varlist coshare col_housing unemployment tanf_uptake tanf_benefit_scaled expend_welfare_pop expend_welfare_poorpop realgdp_percap dem_share_both state_liberalism_median {
egen    gr`x' = cut(`x') , group(5)
replace gr`x' = 99 if gr`x'==0
bys gr`x': sum `x'
}

/////////////////////////////////////////////////
/// match rate to lpr adjustment (fig S1, S2, S3)

foreach y of varlist match1_r_l {
 foreach x of varlist rnat_top rarrivefy rstate {
  preserve
  collapse (mean) `y' (count) obs = `y' , by(`x') 
  outsheet using "matchrate_`y'_`x'.csv", comma replace
  restore
 } 
}

//////////////////////////////////////////////////////////
/// restrict sample to those who match into green card
keep if  match1_r_l == 1

// code months to green card application

//  months from arrival to lpr decision 
gen monthstolpr= floor((ldecdate - rdtarrive)/(365/12))

// months from arrival to lpr submission 
gen monthstolpr_receipt= floor((lrcpt - rdtarrive)/(365/12))

* drop obs with negative times to submission and those who submit before 8 months
drop if monthstolpr_receipt==.
drop if monthstolpr_receipt<8
drop if monthstolpr_receipt>monthstolpr & monthstolpr!=. 

* cases who adjust within 36 months of arrival
gen submitted_36 = monthstolpr_receipt<=36 

/// match rate to lpr adjustment within 36 months
foreach y of varlist submitted_36  {
 foreach x of varlist rarrivefy {
  preserve
  collapse (mean) `y' (count) obs = `y' , by(`x') 
  outsheet using "matchrate_`y'_`x'.csv", comma replace
  restore
 } 
}

//////////////////////////////////////////////
* restrict sample to cases that adjust within 36 months
keep if submitted_36==1

* five bins to control for time to adjustment
egen    grmonthstolpr_receipt = cut(monthstolpr_receipt) , group(5)
replace grmonthstolpr_receipt = 99 if grmonthstolpr_receipt==0

////////////////////////////////////////////////////
/// Analysis of Secondary Migration:

/////// state to state movement (fig 1)
qui: estpost tab rstate lstate , nototal
qui: esttab using "crosstab_rtolstate.csv" , cells(b) nostar  ///
      nonote nomtitle nonumber noobs replace unstack
	  
* by origin and year (fig 3, fig s10, tables s2-5)
tab rnat_top_n rarrivefy_n, mis
levelsof rnat_top_n, local(levels)
levelsof rarrivefy_n, local(freelevels)
 foreach l of local levels {
  foreach k of local freelevels {
   qui: cap estpost tab rstate lstate , nototal , if rnat_top_n==`l' & rarrivefy_n==`k'
   qui: cap esttab using "crosstab_rtolstate_rnat`l'_rarrivefy_n`k'.csv" , cells(b) nostar  ///
      nonote nomtitle nonumber noobs replace unstack
eststo clear
 }
}

* by origin and year (adjusted within 24 months) (fig s11)
levelsof rnat_top_n, local(levels)
levelsof rarrivefy_n, local(freelevels)
 foreach l of local levels {
  foreach k of local freelevels {
   qui: cap estpost tab rstate lstate , nototal , if rnat_top_n==`l' & rarrivefy_n==`k' & monthstolpr_receipt<=24
   qui: cap esttab using "crosstab_rtolstate24_rnat`l'_rarrivefy_n`k'.csv" , cells(b) nostar  ///
      nonote nomtitle nonumber noobs replace unstack
eststo clear
 }
}

///// rates of moving out of state (fig s12)
foreach x of varlist rarrivefy {
preserve
collapse (mean) movedrtol (count) obs = movedrtol , by(`x') 
outsheet using "crosstab_movedrtol_`x'.csv", comma replace
restore
} 

//////////////////
//// summary stats (Table s1)
sum movedrtol ib99.casesize_n ib99.relat_n ib99.rgender_n ib99.reduccat_n ib99.free_n ib99.rage_n ib99.rnat_top_n rarrivefy  

//////////////////////////////
/// regressions: push models 

* individual levels factors 
global x1b = "ib99.relat_n ib99.rgender_n ib99.reduccat_n ib99.rnat_top_n ib99.free_n ib99.rra_n ib99.rage_n ib99.rarrivefy_n ib99.rstate_n ib99.casesize_n ib99.grmonthstolpr_receipt"

* geo factors (without and without time to adjustment)
global x3b   = "$x1b ib99.grcoshare ib99.grcol_housing ib99.grunemployment ib99.grexpend_welfare_poorpop ib99.grrealgdp_percap govparty_dem"
global specs = "x1 x3b"

* big states (fig 2, fig s4)
foreach x of global specs {
reg movedrtol $`x' , cl(rcasenumber) , if rstate_count>1000 & rstate_count!=.
di "Ad. R2: `e(r2_a)'"
eststo m1
esttab m1 using "reg_movedrtol_bigstates_`x'.csv" , replace wide label plain se
eststo clear
}

* by ties or no ties (fig s5, s6)
foreach x of global specs {
 levelsof free_n, local(levels)
 foreach l of local levels {
 cap noisily reg movedrtol $`x' , cl(rcasenumber) , if free_n==`l' 
 cap di "Ad. R2: `e(r2_a)'"
 cap eststo m1
 cap esttab m1 using "reg_movedrtol_free_`l'_`x'.csv" , replace wide label plain se
 eststo clear
 }
}

* by principal applicant (fig s7)
foreach x of global specs {
 levelsof relat_n, local(levels)
 foreach l of local levels {
 cap noisily reg movedrtol $`x' , cl(rcasenumber) , if relat_n==`l'
 cap di "Ad. R2: `e(r2_a)'"
 cap eststo m1
 cap esttab m1 using "reg_movedrtol_relat_`l'_`x'.csv" , replace wide label plain se
 eststo clear
 }
}

* all states sample (fig s8)
foreach x of global specs {
reg movedrtol $`x' , cl(rcasenumber)
di "Ad. R2: `e(r2_a)'"
eststo m1
esttab m1 using "reg_movedrtol_`x'.csv" , replace wide label plain se
eststo clear
}

* adjusted within 24 months (big states) (fig s9)
foreach x of global specs {
reg movedrtol $`x' , cl(rcasenumber) , if rstate_count>1000 & rstate_count!=. & monthstolpr_receipt<=24
di "Ad. R2: `e(r2_a)'"
eststo m1
esttab m1 using "reg_movedrtol24_bigstates_`x'.csv" , replace wide label plain se
eststo clear
}

//////////////////////////////////////////////////////
/// gravity models 

// load flow dataset for big states (created by code_analysis_partB.R)
use "dyadmovesorigin.dta", clear

* merge in arrival state data
rename rstate state 
merge m:1 state year using tomerge_state_gravity.dta
keep if _merge==3
renvars $statevars nat*, prefix(r_)
rename state rstate
drop _merge

* merge in dest state data
rename lstate state
merge m:1 state year using tomerge_state_gravity.dta
keep if _merge==3
renvars $statevars  nat*, prefix(l_)
rename state lstate 
drop _merge

* code origin 
gen     rnat_name=""
replace rnat_name = "Afghanistan"             if rnat==1
replace rnat_name = "Bhutan"                  if rnat==2
replace rnat_name = "Bosnia And Herzegovina"  if rnat==3
replace rnat_name = "Burma"                   if rnat==4
replace rnat_name = "Dem. Rep. Congo"         if rnat==5
replace rnat_name = "Eritrea"                 if rnat==6
replace rnat_name = "Ethiopia"                if rnat==7
replace rnat_name = "Iran"                    if rnat==8
replace rnat_name = "Iraq"                    if rnat==9
replace rnat_name = "Liberia"                 if rnat==10
replace rnat_name = "Russia"                  if rnat==12
replace rnat_name = "Somalia"                 if rnat==13
replace rnat_name = "Sudan"                   if rnat==14
replace rnat_name = "Ukraine"                 if rnat==15
replace rnat_name = "Vietnam"                 if rnat==16
replace rnat_name = "Other"                   if rnat==99


gen     r_coshare = .
replace r_coshare = r_nat_afghan    if rnat_name=="Afghanistan"
replace r_coshare = r_nat_bhutan    if rnat_name=="Bhutan"
replace r_coshare = r_nat_bosnia    if rnat_name=="Bosnia And Herzegovina"
replace r_coshare = r_nat_burma     if rnat_name=="Burma"
replace r_coshare = r_nat_drc       if rnat_name=="Dem. Rep. Congo"
replace r_coshare = r_nat_eritrea   if rnat_name=="Eritrea"
replace r_coshare = r_nat_ethiopia  if rnat_name=="Ethiopia"
replace r_coshare = r_nat_iran      if rnat_name=="Iran"
replace r_coshare = r_nat_iraq      if rnat_name=="Iraq"
replace r_coshare = r_nat_liberia   if rnat_name=="Liberia"
replace r_coshare = r_nat_russia    if rnat_name=="Russia"
replace r_coshare = r_nat_somalia   if rnat_name=="Somalia"
replace r_coshare = r_nat_sudan     if rnat_name=="Sudan"
replace r_coshare = r_nat_ukraine   if rnat_name=="Ukraine"
replace r_coshare = r_nat_vietnam   if rnat_name=="Vietnam"

gen     l_coshare = .
replace l_coshare = l_nat_afghan    if rnat_name=="Afghanistan"
replace l_coshare = l_nat_bhutan    if rnat_name=="Bhutan"
replace l_coshare = l_nat_bosnia    if rnat_name=="Bosnia And Herzegovina"
replace l_coshare = l_nat_burma     if rnat_name=="Burma"
replace l_coshare = l_nat_drc       if rnat_name=="Dem. Rep. Congo"
replace l_coshare = l_nat_eritrea   if rnat_name=="Eritrea"
replace l_coshare = l_nat_ethiopia  if rnat_name=="Ethiopia"
replace l_coshare = l_nat_iran      if rnat_name=="Iran"
replace l_coshare = l_nat_iraq      if rnat_name=="Iraq"
replace l_coshare = l_nat_liberia   if rnat_name=="Liberia"
replace l_coshare = l_nat_russia    if rnat_name=="Russia"
replace l_coshare = l_nat_somalia   if rnat_name=="Somalia"
replace l_coshare = l_nat_sudan     if rnat_name=="Sudan"
replace l_coshare = l_nat_ukraine   if rnat_name=="Ukraine"
replace l_coshare = l_nat_vietnam   if rnat_name=="Vietnam"

drop r_nat* l_nat*

* drop stayers
drop if rstate == lstate

* outcome
gen lnmovers = log(1+movers)

* dyad FEs
egen did = group(rstate lstate)

* bin geographic factors and code differenced versions
global statevars = "state_liberalism_median dem_share_sen dem_share_house col_housing unemployment tanf_uptake_rate tanf_benefit_scaled realgdp_percap inc_cap dem_share_both expend_welfare_pop expend_gen_pop coshare expend_welfare_poorpop foreignborn"

foreach x of global statevars {
gen  lmr_`x'    = l_`x' - r_`x' 
egen gr_l_`x'   = cut(l_`x'), group(5)
egen gr_r_`x'   = cut(r_`x'), group(5)
egen gr_lmr_`x' = cut(lmr_`x'), group(5)
}

* code deltas for gov var
gen  lmr_govparty_dem = l_govparty_dem - r_govparty_dem 
tab  lmr_govparty_dem, gen(lmr_govparty_demdum)

* stock
gen  lnarrivals = log(arrivals)
egen gr_lnarrivals = cut(lnarrivals), group(5)

label var l_govparty_dem "l_govparty_dem"
label var r_govparty_dem "r_govparty_dem"

label define cats 0 "1st Quintile " 1 "2nd Quintile" 2 "3rd Quintile" 3 "4th Quintile" 4 "5th Quintile"
foreach x of varlist gr_l_* gr_r_* gr_l_* gr_lmr_* gr_lnarrivals  {
label values `x' cats
}

label var l_govparty_dem "Destination State: Governor Democrat" 
label var r_govparty_dem "Arrival State: Governor Democrat"

label var lmr_govparty_demdum2 "Difference: Governor Democrat = Medium" 
label var lmr_govparty_demdum3 "Difference: Governor Democrat = High"

/// regression for fig 3
global covars1 =  "coshare col_housing unemployment expend_welfare_poorpop realgdp_percap"
global discrete1 = ""
 foreach x of global covars1 {
  global discrete1 = "$discrete1 i.gr_l_`x' i.gr_r_`x'" 
 } 

eststo clear 
eststo: vce2way xtreg lnmovers $discrete1 l_govparty_dem r_govparty_dem i.gr_lnarrivals i.year, cl(lstate rstate) i(did) fe 
esttab * using "gravity_main.csv" , replace wide nolabel plain se

esttab * using gravity_main.rtf,  ///
 b(3) se(3) noomitted star(* 0.05) nomtitle collabels(none)   varwidth(50) replace ///
 title({\b Table X} {Estimates from Gravity Model with Different Welfare Proxies }) label nogap ///
 refcat(0.gr_l_coshare "Destination State: Share of Co-Nationals" 0.gr_r_coshare "Arrival State: Share of Co-Nationals"  ///
        0.gr_l_col_housing "Destination State: Cost of Housing" 0.gr_r_col_housing "Arrival State: Cost of Housing"  ///
		0.gr_l_unemployment "Destination State: Unemployment" 0.gr_r_unemployment "Arrival State: Unemployment"  ///
		0.gr_l_expend_welfare_poorpop "Destination State: Welfare spending (per poor)" 0.gr_r_expend_welfare_poorpop "Arrival State: Welfare spending (per poor)"  ///
		0.gr_l_realgdp_percap "Destination State: GDP per capita" 0.gr_r_realgdp_percap "Arrival State: GDP per capita"  ///
		0.gr_lnarrivals "Stock of Arrivals"   /// 
		, nolabel)

/// table s2: replicate main model (symmetry)
global discrete3 = ""
 foreach x of global covars1 {
  global discrete3 = "$discrete3 i.gr_lmr_`x'" 
} 

eststo clear 
eststo: vce2way xtreg lnmovers $discrete3 lmr_govparty_demdum2 lmr_govparty_demdum3 i.gr_lnarrivals i.year, cl(lstate rstate) i(did) fe 

esttab * using gravity_delta.rtf,  ///
 b(3) se(3) noomitted star(* 0.05) nomtitle collabels(none)   varwidth(50) replace ///
 title({\b Table X} {Estimates from Gravity Model with Differenced State Predictors  }) label nogap ///
 refcat(0.gr_lmr_coshare "Difference in Share of Co-Nationals" ///
        0.gr_lmr_col_housing "Difference in Cost of Housing"  ///
		0.gr_lmr_unemployment "Difference in  Unemployment"   ///
		0.gr_lmr_expend_welfare_poorpop "Difference in Welfare spending (per poor)"  ///
		0.gr_lmr_realgdp_percap "Difference in GDP per capita"  ///
		0.gr_lnarrivals "Stock of Arrivals"   /// 
 , nolabel)

/// table s3: replicate with alternative welfare measures

* covars list for different welfare proxies
global covars2 =  "coshare col_housing unemployment expend_welfare_pop  realgdp_percap"
global covars3 =  "coshare col_housing unemployment tanf_benefit_scaled realgdp_percap"
global covars4 =  "coshare col_housing unemployment tanf_uptake_rate    realgdp_percap"

eststo clear
 forvalues k = 2/4 {
  global discrete1 = ""
   foreach x of global covars`k' {
    global discrete1 = "$discrete1 i.gr_l_`x' i.gr_r_`x'" 
 } 

eststo: vce2way xtreg lnmovers $discrete1 l_govparty_dem r_govparty_dem i.gr_lnarrivals i.year, cl(lstate rstate) i(did) fe 
}

esttab * using gravity_welfare.rtf,  ///
 b(3) se(3) noomitted star(* 0.05) nomtitle collabels(none)   varwidth(50) replace ///
 title({\b Table X} {Estimates from Gravity Model with Different Welfare Proxies }) label nogap ///
 refcat(0.gr_l_coshare "Destination State: Share of Co-Nationals" 0.gr_r_coshare "Arrival State: Share of Co-Nationals"  ///
        0.gr_l_col_housing "Destination State: Cost of Housing" 0.gr_r_col_housing "Arrival State: Cost of Housing"  ///
		0.gr_l_unemployment "Destination State: Unemployment" 0.gr_r_unemployment "Arrival State: Unemployment"  ///
		0.gr_l_expend_welfare_poorpop "Destination State: Welfare spending (per poor)" 0.gr_r_expend_welfare_poorpop "Arrival State: Welfare spending (per poor)"  ///
		0.gr_l_realgdp_percap "Destination State: GDP per capita" 0.gr_r_realgdp_percap "Arrival State: GDP per capita"  ///
		0.gr_l_expend_welfare_pop "Destination State: Welfare spending (per pop)" 0.gr_r_expend_welfare_pop "Arrival State: Welfare spending (per pop)"  ///
		0.gr_l_tanf_benefit_scaled "Destination State: Tanf Rate" 0.gr_r_tanf_benefit_scaled "Arrival State: Tanf Rate"  ///
		0.gr_l_tanf_uptake_rate "Destination State: Tanf Uptake" 0.gr_r_tanf_uptake_rate "Arrival State: Tanf Uptake"  ///
		0.gr_lnarrivals "Stock of Arrivals"   /// 
 , nolabel)

/// table s4: replicate with alternative political proxies
eststo clear 
* covars list
global covars1 =  "coshare col_housing unemployment expend_welfare_poorpop realgdp_percap state_liberalism_median"
global covars2 =  "coshare col_housing unemployment expend_welfare_poorpop realgdp_percap dem_share_sen"
global covars3 =  "coshare col_housing unemployment expend_welfare_poorpop realgdp_percap dem_share_house"
global covars4 =  "coshare col_housing unemployment expend_welfare_poorpop realgdp_percap dem_share_both"

eststo clear
 forvalues k = 1/4 {
  global discrete1 = ""
   foreach x of global covars`k' {
    global discrete1 = "$discrete1 i.gr_l_`x' i.gr_r_`x'" 
 } 

eststo: vce2way xtreg lnmovers $discrete1 i.gr_lnarrivals i.year, cl(lstate rstate) i(did) fe 
}

esttab * using gravity_pol.rtf,  ///
 b(3) se(3) noomitted star(* 0.05) nomtitle collabels(none)   varwidth(50) replace ///
 title({\b Table X} {Estimates from Gravity Model with Alternative Measures of State Partisanship }) label nogap ///
 refcat(0.gr_l_coshare "Destination State: Share of Co-Nationals" 0.gr_r_coshare "Arrival State: Share of Co-Nationals"  ///
        0.gr_l_col_housing "Destination State: Cost of Housing" 0.gr_r_col_housing "Arrival State: Cost of Housing"  ///
		0.gr_l_unemployment "Destination State: Unemployment" 0.gr_r_unemployment "Arrival State: Unemployment"  ///
		0.gr_l_expend_welfare_poorpop "Destination State: Welfare spending (per poor)" 0.gr_r_expend_welfare_poorpop "Arrival State: Welfare spending (per poor)"  ///
		0.gr_l_realgdp_percap "Destination State: GDP per capita" 0.gr_r_realgdp_percap "Arrival State: GDP per capita"  ///
		0.gr_l_state_liberalism_median "Destination State: Policy Index" 0.gr_r_state_liberalism_median "Arrival State: Policy Index"  ///
		0.gr_l_dem_share_sen "Destination State: Share of Dem Senate Seats" 0.gr_r_dem_share_sen "Arrival State: Share of Dem Senate Seatd"  ///
		0.gr_l_dem_share_house "Destination State: Share of Dem House Seats" 0.gr_r_dem_share_house "Arrival State: Share of Dem House Seatd"  ///
		0.gr_l_dem_share_both "Destination State: Share of Dem Seats" 0.gr_r_dem_share_both "Arrival State: Share of Dem Seats"  ///
		0.gr_lnarrivals "Stock of Arrivals"   /// 
        , nolabel)


/// table s5: with omitting origins 
global covars1 =  "coshare col_housing unemployment expend_welfare_poorpop realgdp_percap"
global discrete1 = ""
 foreach x of global covars1 {
  global discrete1 = "$discrete1 i.gr_l_`x' i.gr_r_`x'" 
 } 

eststo clear 
eststo: vce2way xtreg lnmovers $discrete1 l_govparty_dem r_govparty_dem i.gr_lnarrivals i.year, cl(lstate rstate) i(did) fe  
levelsof rnat, local(levels)
 foreach l of local levels {
  preserve
  keep if rnat!=`l'
  eststo: vce2way xtreg lnmovers $discrete1 l_govparty_dem r_govparty_dem i.gr_lnarrivals i.year , cl(lstate rstate) i(did) fe  
  restore
}

esttab * using gravity_main_omitcountry.rtf,  ///
 b(3) se(3) noomitted star(* 0.05) nomtitle collabels(none)   varwidth(50) replace ///
 title({\b Table X} {Estimates from Gravity Model Omitting Origins }) label nogap ///
 refcat(0.gr_l_coshare "Destination State: Share of Co-Nationals" 0.gr_r_coshare "Arrival State: Share of Co-Nationals"  ///
        0.gr_l_col_housing "Destination State: Cost of Housing" 0.gr_r_col_housing "Arrival State: Cost of Housing"  ///
		0.gr_l_unemployment "Destination State: Unemployment" 0.gr_r_unemployment "Arrival State: Unemployment"  ///
		0.gr_l_expend_welfare_poorpop "Destination State: Welfare spending (per poor)" 0.gr_r_expend_welfare_poorpop "Arrival State: Welfare spending (per poor)"  ///
		0.gr_l_realgdp_percap "Destination State: GDP per capita" 0.gr_r_realgdp_percap "Arrival State: GDP per capita"  ///
		0.gr_lnarrivals "Stock of Arrivals"   /// 
		, nolabel)

		
/// fig s10 with all states
global statevars = "state_liberalism_median dem_share_sen dem_share_house col_housing unemployment tanf_uptake_rate tanf_benefit_scaled realgdp_percap inc_cap dem_share_both govparty_dem expend_welfare_pop expend_gen_pop expend_welfare_poorpop foreignborn"

* load flow dataset for big states (created by code_analysis_partB)
use "dyadmovesorigin_allstates.dta", clear

* merge in arrival state data
rename rstate state 
merge m:1 state year using tomerge_state_gravity.dta
keep if _merge==3
renvars $statevars nat*, prefix(r_)
rename state rstate
drop _merge

* merge in desitantion state data
rename lstate state
merge m:1 state year using tomerge_state_gravity.dta
keep if _merge==3
renvars $statevars  nat*, prefix(l_)
rename state lstate 
drop _merge

* code origin
gen     rnat_name=""
replace rnat_name = "Afghanistan"             if rnat==1
replace rnat_name = "Bhutan"                  if rnat==2
replace rnat_name = "Bosnia And Herzegovina"  if rnat==3
replace rnat_name = "Burma"                   if rnat==4
replace rnat_name = "Dem. Rep. Congo"         if rnat==5
replace rnat_name = "Eritrea"                 if rnat==6
replace rnat_name = "Ethiopia"                if rnat==7
replace rnat_name = "Iran"                    if rnat==8
replace rnat_name = "Iraq"                    if rnat==9
replace rnat_name = "Liberia"                 if rnat==10
replace rnat_name = "Russia"                  if rnat==12
replace rnat_name = "Somalia"                 if rnat==13
replace rnat_name = "Sudan"                   if rnat==14
replace rnat_name = "Ukraine"                 if rnat==15
replace rnat_name = "Vietnam"                 if rnat==16
replace rnat_name = "Other"                   if rnat==99

gen     r_coshare = .
replace r_coshare = r_nat_afghan    if rnat_name=="Afghanistan"
replace r_coshare = r_nat_bhutan    if rnat_name=="Bhutan"
replace r_coshare = r_nat_bosnia    if rnat_name=="Bosnia And Herzegovina"
replace r_coshare = r_nat_burma     if rnat_name=="Burma"
replace r_coshare = r_nat_drc       if rnat_name=="Dem. Rep. Congo"
replace r_coshare = r_nat_eritrea   if rnat_name=="Eritrea"
replace r_coshare = r_nat_ethiopia  if rnat_name=="Ethiopia"
replace r_coshare = r_nat_iran      if rnat_name=="Iran"
replace r_coshare = r_nat_iraq      if rnat_name=="Iraq"
replace r_coshare = r_nat_liberia   if rnat_name=="Liberia"
replace r_coshare = r_nat_russia    if rnat_name=="Russia"
replace r_coshare = r_nat_somalia   if rnat_name=="Somalia"
replace r_coshare = r_nat_sudan     if rnat_name=="Sudan"
replace r_coshare = r_nat_ukraine   if rnat_name=="Ukraine"
replace r_coshare = r_nat_vietnam   if rnat_name=="Vietnam"

gen     l_coshare = .
replace l_coshare = l_nat_afghan    if rnat_name=="Afghanistan"
replace l_coshare = l_nat_bhutan    if rnat_name=="Bhutan"
replace l_coshare = l_nat_bosnia    if rnat_name=="Bosnia And Herzegovina"
replace l_coshare = l_nat_burma     if rnat_name=="Burma"
replace l_coshare = l_nat_drc       if rnat_name=="Dem. Rep. Congo"
replace l_coshare = l_nat_eritrea   if rnat_name=="Eritrea"
replace l_coshare = l_nat_ethiopia  if rnat_name=="Ethiopia"
replace l_coshare = l_nat_iran      if rnat_name=="Iran"
replace l_coshare = l_nat_iraq      if rnat_name=="Iraq"
replace l_coshare = l_nat_liberia   if rnat_name=="Liberia"
replace l_coshare = l_nat_russia    if rnat_name=="Russia"
replace l_coshare = l_nat_somalia   if rnat_name=="Somalia"
replace l_coshare = l_nat_sudan     if rnat_name=="Sudan"
replace l_coshare = l_nat_ukraine   if rnat_name=="Ukraine"
replace l_coshare = l_nat_vietnam   if rnat_name=="Vietnam"

drop r_nat* l_nat*

* drop stayers
drop if rstate == lstate

* outcome
gen lnmovers = log(1+movers)

* dyad FEs
egen did = group(rstate lstate)

* group geo factors
global statevars = "state_liberalism_median dem_share_sen dem_share_house col_housing unemployment tanf_uptake_rate tanf_benefit_scaled realgdp_percap inc_cap dem_share_both expend_welfare_pop expend_gen_pop coshare expend_welfare_poorpop foreignborn"

foreach x of global statevars {
gen  lmr_`x'    = l_`x' - r_`x' 
egen gr_l_`x'   = cut(l_`x'), group(5)
egen gr_r_`x'   = cut(r_`x'), group(5)
egen gr_lmr_`x' = cut(lmr_`x'), group(5)
}

* code deltas for gov var
gen  lmr_govparty_dem = l_govparty_dem - r_govparty_dem 
tab  lmr_govparty_dem, gen(lmr_govparty_demdum)

* stock
gen  lnarrivals = log(arrivals)
egen gr_lnarrivals = cut(lnarrivals), group(5)

label var l_govparty_dem "l_govparty_dem"
label var r_govparty_dem "r_govparty_dem"

label define cats 0 "1st Quintile " 1 "2nd Quintile" 2 "3rd Quintile" 3 "4th Quintile" 4 "5th Quintile"

foreach x of varlist gr_l_* gr_r_* gr_l_* gr_lmr_* gr_lnarrivals  {
label values `x' cats
}

label var l_govparty_dem "Destination State: Governor Democrat" 
label var r_govparty_dem "Arrival State: Governor Democrat"

label var lmr_govparty_demdum2 "Difference: Governor Democrat = Medium" 
label var lmr_govparty_demdum3 "Difference: Governor Democrat = High"


/// Estimates for fig s10
global covars1 =  "coshare col_housing unemployment expend_welfare_poorpop realgdp_percap"
global discrete1 = ""
 foreach x of global covars1 {
  global discrete1 = "$discrete1 i.gr_l_`x' i.gr_r_`x'" 
 } 

eststo clear 
eststo: vce2way xtreg lnmovers $discrete1 l_govparty_dem r_govparty_dem i.gr_lnarrivals i.year, cl(lstate rstate) i(did) fe 
esttab * using "gravity_main_allstates.csv" , replace wide nolabel plain se

esttab * using gravity_main_allstates.rtf,  ///
 b(3) se(3) noomitted star(* 0.05) nomtitle collabels(none)   varwidth(50) replace ///
 title({\b Table X} {Estimates from Gravity Model with Different Welfare Proxies }) label nogap ///
 refcat(0.gr_l_coshare "Destination State: Share of Co-Nationals" 0.gr_r_coshare "Arrival State: Share of Co-Nationals"  ///
        0.gr_l_col_housing "Destination State: Cost of Housing" 0.gr_r_col_housing "Arrival State: Cost of Housing"  ///
		0.gr_l_unemployment "Destination State: Unemployment" 0.gr_r_unemployment "Arrival State: Unemployment"  ///
		0.gr_l_expend_welfare_poorpop "Destination State: Welfare spending (per poor)" 0.gr_r_expend_welfare_poorpop "Arrival State: Welfare spending (per poor)"  ///
		0.gr_l_realgdp_percap "Destination State: GDP per capita" 0.gr_r_realgdp_percap "Arrival State: GDP per capita"  ///
		0.gr_lnarrivals "Stock of Arrivals"   /// 
		, nolabel)

		
/// analysis for s11 those who applied for LPR Status within 24 Months. 
global statevars = "state_liberalism_median dem_share_sen dem_share_house col_housing unemployment tanf_uptake_rate tanf_benefit_scaled realgdp_percap inc_cap dem_share_both govparty_dem expend_welfare_pop expend_gen_pop expend_welfare_poorpop foreignborn"

* load flow dataset for those who adjusted within 24 months (created by code_analysis_partB.R)
use "dyadmovesorigin24.dta", clear

* merge in arrival state data
rename rstate state 
merge m:1 state year using tomerge_state_gravity.dta
keep if _merge==3
renvars $statevars nat*, prefix(r_)
rename state rstate
drop _merge

* merge in desitantion state data
rename lstate state
merge m:1 state year using tomerge_state_gravity.dta
keep if _merge==3
renvars $statevars  nat*, prefix(l_)
rename state lstate 
drop _merge

* code origin
gen     rnat_name=""
replace rnat_name = "Afghanistan"             if rnat==1
replace rnat_name = "Bhutan"                  if rnat==2
replace rnat_name = "Bosnia And Herzegovina"  if rnat==3
replace rnat_name = "Burma"                   if rnat==4
replace rnat_name = "Dem. Rep. Congo"         if rnat==5
replace rnat_name = "Eritrea"                 if rnat==6
replace rnat_name = "Ethiopia"                if rnat==7
replace rnat_name = "Iran"                    if rnat==8
replace rnat_name = "Iraq"                    if rnat==9
replace rnat_name = "Liberia"                 if rnat==10
replace rnat_name = "Russia"                  if rnat==12
replace rnat_name = "Somalia"                 if rnat==13
replace rnat_name = "Sudan"                   if rnat==14
replace rnat_name = "Ukraine"                 if rnat==15
replace rnat_name = "Vietnam"                 if rnat==16
replace rnat_name = "Other"                   if rnat==99


gen     r_coshare = .
replace r_coshare = r_nat_afghan    if rnat_name=="Afghanistan"
replace r_coshare = r_nat_bhutan    if rnat_name=="Bhutan"
replace r_coshare = r_nat_bosnia    if rnat_name=="Bosnia And Herzegovina"
replace r_coshare = r_nat_burma     if rnat_name=="Burma"
replace r_coshare = r_nat_drc       if rnat_name=="Dem. Rep. Congo"
replace r_coshare = r_nat_eritrea   if rnat_name=="Eritrea"
replace r_coshare = r_nat_ethiopia  if rnat_name=="Ethiopia"
replace r_coshare = r_nat_iran      if rnat_name=="Iran"
replace r_coshare = r_nat_iraq      if rnat_name=="Iraq"
replace r_coshare = r_nat_liberia   if rnat_name=="Liberia"
replace r_coshare = r_nat_russia    if rnat_name=="Russia"
replace r_coshare = r_nat_somalia   if rnat_name=="Somalia"
replace r_coshare = r_nat_sudan     if rnat_name=="Sudan"
replace r_coshare = r_nat_ukraine   if rnat_name=="Ukraine"
replace r_coshare = r_nat_vietnam   if rnat_name=="Vietnam"

gen     l_coshare = .
replace l_coshare = l_nat_afghan    if rnat_name=="Afghanistan"
replace l_coshare = l_nat_bhutan    if rnat_name=="Bhutan"
replace l_coshare = l_nat_bosnia    if rnat_name=="Bosnia And Herzegovina"
replace l_coshare = l_nat_burma     if rnat_name=="Burma"
replace l_coshare = l_nat_drc       if rnat_name=="Dem. Rep. Congo"
replace l_coshare = l_nat_eritrea   if rnat_name=="Eritrea"
replace l_coshare = l_nat_ethiopia  if rnat_name=="Ethiopia"
replace l_coshare = l_nat_iran      if rnat_name=="Iran"
replace l_coshare = l_nat_iraq      if rnat_name=="Iraq"
replace l_coshare = l_nat_liberia   if rnat_name=="Liberia"
replace l_coshare = l_nat_russia    if rnat_name=="Russia"
replace l_coshare = l_nat_somalia   if rnat_name=="Somalia"
replace l_coshare = l_nat_sudan     if rnat_name=="Sudan"
replace l_coshare = l_nat_ukraine   if rnat_name=="Ukraine"
replace l_coshare = l_nat_vietnam   if rnat_name=="Vietnam"
drop r_nat* l_nat*

* drop stayers
drop if rstate == lstate

* outcome
gen lnmovers = log(1+movers)

* dyad FEs
egen did = group(rstate lstate)

* group geo factors
global statevars = "state_liberalism_median dem_share_sen dem_share_house col_housing unemployment tanf_uptake_rate tanf_benefit_scaled realgdp_percap inc_cap dem_share_both expend_welfare_pop expend_gen_pop coshare expend_welfare_poorpop foreignborn"

foreach x of global statevars {
gen  lmr_`x'    = l_`x' - r_`x' 
egen gr_l_`x'   = cut(l_`x'), group(5)
egen gr_r_`x'   = cut(r_`x'), group(5)
egen gr_lmr_`x' = cut(lmr_`x'), group(5)
}

* code deltas for gov var
gen  lmr_govparty_dem = l_govparty_dem - r_govparty_dem 
tab  lmr_govparty_dem, gen(lmr_govparty_demdum)

* stock
gen  lnarrivals = log(arrivals)
egen gr_lnarrivals = cut(lnarrivals), group(5)

label var l_govparty_dem "l_govparty_dem"
label var r_govparty_dem "r_govparty_dem"

label define cats 0 "1st Quintile " 1 "2nd Quintile" 2 "3rd Quintile" 3 "4th Quintile" 4 "5th Quintile"

foreach x of varlist gr_l_* gr_r_* gr_l_* gr_lmr_* gr_lnarrivals  {
label values `x' cats
}

label var l_govparty_dem "Destination State: Governor Democrat" 
label var r_govparty_dem "Arrival State: Governor Democrat"

label var lmr_govparty_demdum2 "Difference: Governor Democrat = Medium" 
label var lmr_govparty_demdum3 "Difference: Governor Democrat = High"


/// estimates for figure s11 
global covars1 =  "coshare col_housing unemployment expend_welfare_poorpop realgdp_percap"
global discrete1 = ""
 foreach x of global covars1 {
  global discrete1 = "$discrete1 i.gr_l_`x' i.gr_r_`x'" 
 } 

eststo clear 
eststo: vce2way xtreg lnmovers $discrete1 l_govparty_dem r_govparty_dem i.gr_lnarrivals i.year, cl(lstate rstate) i(did) fe 
esttab * using "gravity_main_24months.csv" , replace wide nolabel plain se

esttab * using gravity_main_allstates.rtf,  ///
 b(3) se(3) noomitted star(* 0.05) nomtitle collabels(none)   varwidth(50) replace ///
 title({\b Table X} {Estimates from Gravity Model with Different Welfare Proxies }) label nogap ///
 refcat(0.gr_l_coshare "Destination State: Share of Co-Nationals" 0.gr_r_coshare "Arrival State: Share of Co-Nationals"  ///
        0.gr_l_col_housing "Destination State: Cost of Housing" 0.gr_r_col_housing "Arrival State: Cost of Housing"  ///
		0.gr_l_unemployment "Destination State: Unemployment" 0.gr_r_unemployment "Arrival State: Unemployment"  ///
		0.gr_l_expend_welfare_poorpop "Destination State: Welfare spending (per poor)" 0.gr_r_expend_welfare_poorpop "Arrival State: Welfare spending (per poor)"  ///
		0.gr_l_realgdp_percap "Destination State: GDP per capita" 0.gr_r_realgdp_percap "Arrival State: GDP per capita"  ///
		0.gr_lnarrivals "Stock of Arrivals"   /// 
		, nolabel)



